<?php


namespace App\Imports;


use App\Models\AssetAffiliation;
use App\Models\AssetBrand;
use App\Models\AssetKeeper;
use App\Models\AssetLocation;
use App\Models\AssetVendor;

trait AssetImportTrait
{

    protected $newCreatedBrand = [];
    protected $newCreatedKeepers = [];
    protected $newCreatedLocations = [];
    protected $newCreatedVendors = [];
    protected $newCreatedAffiliations = [];

    protected function getOrNewBrand($name)
    {
        if (!empty($this->newCreatedBrand[$name])) {
            return $this->newCreatedBrand[$name];
        }
        $brand = $this->getBrandByName($name);
        if (empty($brand)) {
            $brand = AssetBrand::create([
                "name" => $name
            ]);
            $this->newCreatedBrand[$name] = $brand;
        }
        return $brand;
    }

    protected function getOrNewKeeper($name)
    {
        if (!empty($this->newCreatedKeepers[$name])) {
            return $this->newCreatedKeepers[$name];
        }
        $keeper = $this->getKeeperByName($name);
        if (empty($keeper)) {
            $keeper = AssetKeeper::create([
                "name" => $name
            ]);
            $this->newCreatedKeepers[$name] = $keeper;
        }
        return $keeper;
    }

    protected function getOrNewAffiliation($name)
    {
        if (!empty($this->newCreatedAffiliations[$name])) {
            return $this->newCreatedAffiliations[$name];
        }
        $affiliation = $this->getAffiliationByName($name);
        if (empty($affiliation)) {
            $affiliation = AssetAffiliation::create([
                "name" => $name
            ]);
            $this->newCreatedAffiliations[$name] = $affiliation;
        }
        return $affiliation;
    }

    protected function getOrNewLocation($name)
    {
        if (!empty($this->newCreatedLocations[$name])) {
            return $this->newCreatedLocations[$name];
        }
        $location = $this->getLocationByName($name);
        if (empty($location)) {
            $location = AssetLocation::create([
                "name" => $name
            ]);
            $this->newCreatedLocations[$name] = $location;
        }
        return $location;
    }

    protected function getOrNewVendor($name)
    {
        if (!empty($this->newCreatedVendors[$name])) {
            return $this->newCreatedVendors[$name];
        }
        $vendor = $this->getVendorByName($name);
        if (empty($vendor)) {
            $vendor = AssetVendor::create([
                "name" => $name
            ]);
            $this->newCreatedVendors[$name] = $vendor;
        }
        return $vendor;
    }
}